<?php
/*
 * @Description    : 网站管理
 * @Version        : 1.0.0
 * @Author         : QianLong
 * @Date           : 3022-05-08 15:248
 * @LastEditors    : QianLong
 * @LastEditTime   : 2023-03-27 15:55:06
 */

namespace app\spider\controller;

use app\BaseController;
use app\common\RedisCache;
use app\lib\exception\ApiException;
use app\lib\Result;
use app\common\service\QlRedis;

class Index extends BaseController
{
    protected $redis;
    protected $redisCache;
    public function __construct()
    {
        $remoteIp = $this->getRealIP();
        $this->redis = new QlRedis;
        $this->redisCache = new RedisCache();
        $sysSetting = $this->redisCache->getSysSetting();
        $server_ip = explode(',', $sysSetting['server_ip']);
        if (!in_array($remoteIp, $server_ip)) {
            echo 'error in ' . $remoteIp;
            exit;
        }
    }
    public function getDevKey()
    {
        $uid = input('param.id');
        $setting = $this->redisCache->getUserSetting($uid);
        return Result::Success($setting['dev_key']);
    }
    public function getPushKey()
    {
        $id = input('param.id');
        $setting = $this->redisCache->getPushServerInfo($id);
        return Result::Success($setting['server_secret']);
    }
    function getRealIP()
    {
        $forwarded = request()->header("x-forwarded-for");
        if ($forwarded) {
            $ip = explode(',', $forwarded)[0];
        } else {
            $ip = request()->ip();
        }
        return $ip;
    }
}
